package com.lt.system;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;

import com.lt.common.DebugHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DynamicShortcutHelper
{
    private static DynamicShortcutHelper instance;

    private DynamicShortcutHelper()
    {
    }

    public static DynamicShortcutHelper getInstance()
    {
        if (instance == null)
        {
            synchronized (DynamicShortcutHelper.class)
            {
                instance = new DynamicShortcutHelper();
            }
        }
        return instance;
    }

    /**
     * 创建自定义动态快捷方式
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    public void addCustomShortcut(Context context,
                                  String id,
                                  String shortLabel,
                                  String longLabel,
                                  Icon icon,
                                  Class<?> targetActivity,
                                  HashMap<String, Object> params)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
        {
            return;
        }

        try
        {
            ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
            // 检查是否已达到最大数量
            if (shortcutManager.getDynamicShortcuts().size() >= shortcutManager.getMaxShortcutCountPerActivity())
            {
                // 移除最旧的快捷方式
                List<ShortcutInfo> currentShortcuts = new ArrayList<>(shortcutManager.getDynamicShortcuts());
                if (!currentShortcuts.isEmpty())
                {
                    currentShortcuts.remove(0);
                    shortcutManager.setDynamicShortcuts(currentShortcuts);
                }
            }

            ShortcutInfo shortcut = buildShortcutInfo(context, id, shortLabel, longLabel, icon, targetActivity, params);
            shortcutManager.addDynamicShortcuts(java.util.Collections.singletonList(shortcut));


            // 立即检查是否添加成功
//            List<ShortcutInfo> current = shortcutManager.getDynamicShortcuts();
//            boolean success = current.stream().anyMatch(s -> s.getId().equals(id));
//
//            Log.d(TAG, "Test shortcut creation: " + (success ? "SUCCESS" : "FAILED"));
        } catch (Exception e)
        {
            DebugHelper.e("Error adding custom shortcut: " + e.getMessage());
        }
    }


    /**
     * 构建快捷方式信息
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    private ShortcutInfo buildShortcutInfo(Context context,
                                           String id,
                                           String shortLabel,
                                           String longLabel,
                                           Icon icon,
                                           Class<?> targetActivity,
                                           HashMap<String, Object> params)
    {
        Intent intent = new Intent(context, targetActivity);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra("source", "dynamic_shortcut");
        intent.putExtra("shortcut_id", id);

        // 添加自定义参数
        for (Map.Entry<String, Object> entry : params.entrySet())
        {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (value instanceof String)
            {
                intent.putExtra(key, (String) value);
            } else if (value instanceof Integer)
            {
                intent.putExtra(key, (Integer) value);
            } else if (value instanceof Long)
            {
                intent.putExtra(key, (Long) value);
            } else if (value instanceof Boolean)
            {
                intent.putExtra(key, (Boolean) value);
            } else if (value instanceof Float)
            {
                intent.putExtra(key, (Float) value);
            } else if (value instanceof Double)
            {
                intent.putExtra(key, (Double) value);
            }
        }

        return new ShortcutInfo.Builder(context, id)
                .setShortLabel(shortLabel)
                .setLongLabel(longLabel)
                .setIcon(icon)
                .setIntent(intent)
                .build();
    }


    /**
     * 移除动态快捷方式
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    public void removeDynamicShortcut(Context context, String shortcutId)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
        {
            return;
        }

        try
        {
            ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
            shortcutManager.removeDynamicShortcuts(java.util.Collections.singletonList(shortcutId));
        } catch (Exception e)
        {
            DebugHelper.e("Error removing shortcut: " + e.getMessage());
        }
    }

    /**
     * 禁用快捷方式
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    public void disableShortcut(Context context, String shortcutId, String message)
    {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
        {
            return;
        }

        try
        {
            ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
            shortcutManager.disableShortcuts(java.util.Collections.singletonList(shortcutId), message);
        } catch (Exception e)
        {
            DebugHelper.e("Error disabling shortcut: " + e.getMessage());
        }
    }

    @TargetApi(Build.VERSION_CODES.N_MR1)
    public boolean checkShortcut(Context ctx, String id)
    {
        ShortcutManager shortcutManager = ctx.getSystemService(ShortcutManager.class);
        List<ShortcutInfo> current = shortcutManager.getDynamicShortcuts();
        return current.stream().anyMatch(s -> s.getId().equals(id));
    }
}
